const app = getApp();
Page({
  data: {

  },
  onShow: function () {
    if (!this.data.key) {
      wx.getStorage({
        key: 'key',
        success: res => {
          this.setData({
            key: res.data
          })
        }
      })
    }
  },
  onLoad: function (options) {
    var self = this;
    wx.getStorage({
      key: 'key',
      success: res => {
        self.setData({
          key: res.data
        })
      }
    })
    this.setData({
      rechargecard_id: options.rechargecard_id
    })
    this.getRechargecardInfo();
  },
  getRechargecardInfo: function () {
    var self = this;
    wx.request({
      url: app.globalData.ApiUrl + '/index.php?act=rechargecard&op=rechargecard_qrcode',
      data: {
        rechargecard_id: self.data.rechargecard_id
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: res => {
        if (res.data.code == 200) {
          self.setData({
            rechargecardInfo: res.data.datas
          })
        } else {
          wx.showToast({
            title: res.data.datas.error,
            icon: 'none'
          })
        }
      }
    })
  },
  getRechargecard: function () {
    var self = this;
    if (!self.data.key) {
      if (app.globalData.authorized) {
        wx.navigateTo({
          url: "../chooselogin/chooselogin"
        })
      } else {
        wx.navigateTo({
          url: "../login/login"
        })
      }
      return;
    }
    wx.request({
      url: app.globalData.ApiUrl + '/index.php?act=member_fund&op=get_rechargecard',
      data: {
        key: self.data.key,
        rechargecard_id: self.data.rechargecard_id
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: res => {
        if (res.data.code == 200) {
          wx.showToast({
            title: '领取成功！',
            icon: 'none'
          })
          setTimeout(function () {
            wx.redirectTo({
              url: '../property_card/card'
            })
          }, 1500)
        } else {
          wx.showToast({
            title: res.data.datas.error,
            icon: 'none'
          })
        }
      }
    })
  }
})